package com.newcoin.chat.dao;

import com.newcoin.chat.beans.entity.*;
import com.newcoin.chat.beans.vo.*;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Component
public interface GroupDao {
    /**
     * 创建群聊  获取用户昵称
     *
     * @param userId
     * @return
     */
    @Select("SELECT nickname,head_img_path AS headImgPath FROM chat_user WHERE id=#{userId}")
    UserEntity getNicknameAndHeadImgPathById(@Param("userId") long userId);

    /**
     * 新增群聊
     *
     * @param groupInfoEntity
     * @return
     */
    int addGroup(GroupInfoEntity groupInfoEntity);

    /**
     * 修改群名称
     *
     * @param groupInfoEntity
     * @return
     */
    @Update("UPDATE group_info SET group_name=#{groupName} WHERE id=#{id}")
    int modifyGroupName(GroupInfoEntity groupInfoEntity);

    /**
     * 填入群二维码
     *
     * @param groupId
     * @param QrPath
     * @return
     */
    @Update("UPDATE group_info SET group_qr_code=#{QrPath} WHERE id=#{groupId}")
    int addGroupQRCode(@Param("groupId") long groupId, @Param("QrPath") String QrPath);

    /**
     * 新增群成员关系
     *
     * @param groupId
     * @param userId
     * @return
     */
    @Insert("INSERT INTO group_member (group_id,user_id) VALUES (#{groupId},#{userId})")
    int addGroupMember(@Param("groupId") long groupId, @Param("userId") long userId);

    /**
     * 新增群邀请通知
     *
     * @param groupApplicationEntity
     * @return
     */
    @Insert("INSERT INTO group_application (group_id,group_name,from_user,to_user,state) VALUES (#{groupId},#{groupName},#{fromUser},#{toUser},#{state})")
    int addGroupInviteMessage(GroupApplicationEntity groupApplicationEntity);

    /**
     * 是否有新的消息
     *
     * @param userId
     * @return
     */
    @Select("SELECT COUNT(*) FROM group_application WHERE to_user=#{userId} AND state=0")
    int isThereANewGroupApplication(@Param("userId") long userId);

    /**
     * 获取最近10条群邀请
     *
     * @param userId
     * @return
     */
    List<GroupApplicationEntity> getGroupInviteMessages(@Param("userId") long userId);

    /**
     * 获取所有的好友申请信息
     *
     * @param pageParamVo
     * @return
     */
    List<GroupApplicationEntity> getAllGroupApplicationListById(PageParamVo pageParamVo);

    /**
     * 获取群邀请总条数
     *
     * @param userId
     * @return
     */
    @Select("SELECT COUNT(*) FROM group_application WHERE to_user=#{userId}")
    int getAllGroupApplicationCount(@Param("userId") long userId);

    /**
     * 将未读的消息状态改为已读状态/通过或者拒绝
     *
     * @param userId
     * @return
     */
    @Update("UPDATE group_application SET state=#{state} WHERE id=#{userId}")
    int updateGroupApplicationStateById(@Param("userId") long userId, @Param("state") int state);

    /**
     * 获取群邀请信息
     *
     * @param id
     * @return
     */
    @Select("SELECT id,group_id AS groupId,group_name AS groupName,from_user AS fromUser,to_user AS toUser,state,DATE_FORMAT(create_date,'%Y-%m-%d %H:%i:%s') AS createDate FROM group_application WHERE id=#{id}")
    GroupApplicationEntity getGroupApplicationInfo(@Param("id") long id);

    /**
     * 根据群主id和群id获取群信息
     *
     * @param userId
     * @param groupId
     * @return
     */
    @Select("SELECT id,group_owner AS groupOwner, group_name AS groupName,group_sign AS groupSign,group_qr_code AS groupQrCode,is_open_burn_down AS isOpenBurnDown,DATE_FORMAT(create_date,'%Y-%m-%d %H:%i:%s') AS createDate FROM group_info WHERE id=#{groupId} AND group_owner=#{userId}")
    GroupInfoEntity getGroupInfoByOwnerAndGroupId(@Param("userId") long userId, @Param("groupId") long groupId);

    /**
     * 根据群id获取群信息
     *
     * @param groupId
     * @return
     */
    @Select("SELECT id,group_owner AS groupOwner, group_name AS groupName,group_sign AS groupSign,group_qr_code AS groupQrCode,is_open_burn_down AS isOpenBurnDown,DATE_FORMAT(create_date,'%Y-%m-%d %H:%i:%s') AS createDate FROM group_info WHERE id=#{groupId}")
    GroupInfoEntity getGroupInfoByGroupId(@Param("groupId") long groupId);

    /**
     * 移除群成员或者退出群聊
     *
     * @param groupMemberId
     * @param groupId
     * @return
     */
    @Delete("DELETE FROM group_member WHERE group_id=#{groupId} AND user_id=#{groupMemberId}")
    int removeGroupMember(@Param("groupMemberId") long groupMemberId, @Param("groupId") long groupId);

    /**
     * 群主打开或关闭阅后即焚
     *
     * @param groupId
     * @param state
     * @return
     */
    @Update("UPDATE group_info SET is_open_burn_down=#{state} WHERE id=#{groupId}")
    int updateGroupState(@Param("groupId") long groupId, @Param("state") long state);

    /**
     * 新增群系统消息
     *
     * @param groupMsgEntity
     * @return
     */
    int addGroupSystemMessage(GroupMsgEntity groupMsgEntity);


    /**
     * 根据群id获取群信息
     *
     * @param groupId
     * @return
     */
//    @Select("SELECT id,group_owner AS groupOwner, group_name AS groupName,group_sign AS groupSign,group_qr_code AS groupQrCode,is_open_burn_down AS isOpenBurnDown,group_head_img_path AS groupHeadImgPath  FROM group_info WHERE id=#{groupId}")
    GroupInfoVo getGroupInfoVoByGroupId(@Param("groupId") long groupId);

    /**
     * 获取群成员
     *
     * @param groupId
     * @return
     */
    @Select("SELECT a.nickname,a.head_img_path AS headImgPath,a.id FROM chat_user a,group_member b WHERE b.group_id=#{groupId} AND b.user_id=a.id")
    List<UserVo> getGroupMember(@Param("groupId") Long groupId);


    /**
     * 获取群组阅后即焚的状态
     *
     * @param groupId
     * @return
     */
    Integer getAfterReadingIncinerationState(@Param("groupId") Long groupId);


    /**
     * 获取离线消息
     *
     * @param userId
     * @return
     */
    @Select("select last_message_id from group_member where user_id=#{userId} and group_id=#{groupId} ")
    Long getGroupIdOfflineMsg(@Param("userId") long userId, @Param("groupId") long groupId);

    /**
     * 获取群离线消息
     *
     * @param id
     * @param groupId
     * @return
     */
    List<GroupOfflineMsgVo> getGroupIdOfflineMsgVo(@Param("id") long id, @Param("groupId") long groupId);


    /**
     * 清空未读消息条数
     *
     * @param userId
     * @param groupId
     * @return
     */
    int updateOfflineMsg(@Param("userId") long userId, @Param("groupId") long groupId);

    /**
     * 获取用户是否再群组中
     *
     * @param groupId
     * @param userId
     * @return
     */
    int getGroupMemberIsPresence(@Param("groupId") Long groupId, @Param("userId") Long userId);

    /**
     * 修改群名
     *
     * @param groupId
     * @param groupName
     * @return
     */
    int updateGroupName(@Param("groupId") Long groupId, @Param("groupName") String groupName);

    /**
     * 获取用户的所有群
     *
     * @param userId
     * @return
     */
    List<GroupMemberEntity> getGroupMemberByUserId(@Param("userId") Long userId);

    /**
     * 获取群组最后一条消息的id
     *
     * @param groupId
     * @return
     */
    Long getGroupLastMsgIdByGroupId(@Param("groupId") Long groupId);

    /**
     * 更新用户在该群
     *
     * @param groupMemberId
     * @return
     */
    int updateUserGroupLastMsgId(@Param("groupMemberId") Long groupMemberId, @Param("groupLastMsgId") Long groupLastMsgId);

}
